package com.eugene.process;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.eugene.element.FnCalendar;
import com.martiansoftware.jsap.JSAP;
import com.martiansoftware.jsap.JSAPResult;

public class ProcessTrigger {
	
	private final static Logger logger = 
		LoggerFactory.getLogger(ProcessTrigger.class);
	
	public static void main(String[] args) throws Exception {
		int i;
		String date = null;
		FnCalendar fromDate = null;
		FnCalendar baseDate = null;
		String sceSetId = null;
		String[] fnProdIds ;
//		List<String> fnProdId = new ArrayList<String>();
		try {
			Class clazz = Class.forName("com.eugene.process.ProcessTrigger");
			JSAP jsap = new JSAP(clazz.getResource("/ParserConfig.jsap")) ;
			JSAPResult config = jsap.parse(args);

			if (!config.success()) {
				for (Iterator errs = config.getErrorMessageIterator(); errs.hasNext();) {
					logger.error("Error: {}",  errs.next());
				}
				logger.error("Usage of {} : {}", clazz.getName(),jsap.getUsage() );
				logger.error("{}", jsap.getHelp() );
				System.exit(1);
			}
			
			if( config.contains("fromDate")){
				date = config.getString("fromDate");
				fromDate =  new FnCalendar(
									Integer.parseInt(date.substring(0, 4)),
										Integer.parseInt(date.substring(4, 6))-1,
											Integer.parseInt(date.substring(6, 8))
					   						);
				logger.info("FromDate :{} ", fromDate);
			}
			if( config.contains("baseDate")){
				date = config.getString("baseDate");
				baseDate =  new FnCalendar(
									Integer.parseInt(date.substring(0, 4)),
										Integer.parseInt(date.substring(4, 6))-1,
											Integer.parseInt(date.substring(6, 8))
					   						);
				logger.info("BaseDate :{} ", baseDate);
			}
			if( config.contains("scenario")){
				sceSetId = config.getString("scenario");
				logger.info("Scenario :{} ", sceSetId);
			}
			if( config.contains("name")){
				fnProdIds =config.getStringArray("name");
				logger.info("Count of FnProdId :{} ", fnProdIds.length);
				logger.info("FnProdIds :{} ", fnProdIds);
			}
			
			if(config.contains("makeTS")){
			}
			
			if (config.contains("writeSce")){
//				WriteScenario.writeScenario(sceSetId, "SHIFT_2", baseDate);
//				WriteScenario.writeScenario(sceSetId, "STEEP_1", baseDate);
				if (sceSetId == null) { 
					WriteScenario.writeScenario( baseDate);
				}
				else WriteScenario.writeScenario(sceSetId, baseDate);
				
				logger.info("writeSce :{},{} ", sceSetId, baseDate );
			}
			
//			runMain()

		}catch(ClassNotFoundException e){
			logger.info("Class not found");
		}
		

	}
	
}
